package com.lijiajian.controller;

import com.lijiajian.Service.CustomerServeService;
import com.lijiajian.Service.UserService;
import com.lijiajian.base.BaseController;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.pojo.CustomerServe;
import com.lijiajian.query.CustomerServeQuery;
import com.lijiajian.utils.LoginUserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("customer_serve")
public class CustomerServeController extends BaseController {
    @Resource
    private CustomerServeService customerServeService;
    @Resource
    private UserService userService;

    @GetMapping("selectDicByName")
    @ResponseBody
    public Map<String,Integer> selectDicByName(){
        Map<String,Integer> dicValues=new HashMap<>();
        dicValues.put("咨询",6);
        dicValues.put("建议",7);
        dicValues.put("投诉",8);
        return dicValues;
    }

    /**
     *  查询客户服务数据
     * @param customerServeQuery
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomerServeByParams(CustomerServeQuery customerServeQuery){
            return customerServeService.queryCustomerServeByParams(customerServeQuery);
    }

    @GetMapping("index/{type}")
    public String index(@PathVariable Integer type) {
        if (type != null) {
            if (type == 1) {
                //服务创建
                return "customerServe/customerServe";
            } else if (type == 2) {
                return "customerServe/customerServeAssigner";

            } else if (type == 3) {
                return "customerServe/customerServeHandle";
            } else if (type == 4) {
                return "customerServe/customerServeBack";
            } else if (type == 5) {
                return "customerServe/customerServeArchive";
            }

        }
        else {
            return "";
        }
        return "";

    }


    @PostMapping("add")
    @ResponseBody
    public ResultInfo addCustomerServe(CustomerServe customerServe){
        customerServeService.addCustomerServe(customerServe);
        return success("添加成功");
    }



    /**
     * 审核服务
     * @return
     */
    @PostMapping("updateFlag")
    @ResponseBody
    public ResultInfo updateFlag(String state,Integer id){
        customerServeService.updateFlag(state,id);
        return success("审核成功");
    }

    /**
     * 前往修改服务创建页面
     * @return
     */
    @GetMapping("toAddAndUpdatePage")
    public String toAddAndUpdatePage(Integer id, HttpServletRequest request){
        if(id!=null){
            request.setAttribute("customerServe",customerServeService.selectByPrimaryKey(id));
        }
        return "customerServe/addAndUpdate";
    }

    /**
     * 前往服务分配页面
     * @return
     */
    @GetMapping("toCustomerServeAssignerForEdit")
    public String toCustomerServeAssignerForEdit(Integer id, HttpServletRequest request){
        if(id!=null){
            request.setAttribute("customerServe",customerServeService.selectByPrimaryKey(id));
        }
        return "customerServe/customerServeAssignerForEdit";
    }

    /**
     * 前往服务处理页面
     * @return
     */
    @GetMapping("toCustomerServeHandleForEdit")
    public String toCustomerServeHandleForEdit(Integer id, HttpServletRequest request){
        if(id!=null){
            request.setAttribute("customerServe",customerServeService.selectByPrimaryKey(id));
        }
        return "customerServe/customerServeHandleForEdit";
    }

    /**
     * 前往服务反馈页面
     * @return
     */
    @GetMapping("toCustomerServeBackPage")
    public String toCustomerServeBackPage(Integer id, HttpServletRequest request){
        if(id!=null){
            request.setAttribute("customerServe",customerServeService.selectByPrimaryKey(id));
        }
        return "customerServe/customerServeBackForEdit";
    }


    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCustomerServe(CustomerServe customerServe){
        customerServeService.updateCustomerServe(customerServe);
        return success("修改成功");
    }

    /**
     * 服务分配
     * @param customerServe
     * @return
     */
    @PostMapping("assign")
    @ResponseBody
    public ResultInfo assign(CustomerServe customerServe){
        customerServeService.updateCustomerServe(customerServe);
        return success("修改成功");
    }

    /**
     * 服务处理
     * @param customerServe
     * @return
     */
    @PostMapping("handle")
    @ResponseBody
    public ResultInfo handle(CustomerServe customerServe){

//        customerServe.setServiceProcePeople();
        customerServeService.updateCustomerServe(customerServe);
        return success("修改成功");
    }

    /**
     * 服务反馈
     * @param customerServe
     * @return
     */
    @PostMapping("back")
    @ResponseBody
    public ResultInfo back(CustomerServe customerServe){

        customerServeService.updateCustomerServe(customerServe);
        return success("修改成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomerServe(Integer id){
        customerServeService.deleteCustomerServe(id);
        return success("删除成功");
    }

    /**
     *  查询审核通过的客户服务数据(服务状态为fw_001)
     * @param customerServeQuery
     * @return
     */
    @GetMapping("pass")
    @ResponseBody
    public Map<String,Object> queryCustomerServeByFlagAndState(CustomerServeQuery customerServeQuery){
        return customerServeService.queryCustomerServeByParams(customerServeQuery);
    }

    /**
     *  查询服务状态为fw_002,且指派人为当前用户
     * @param customerServeQuery
     * @return
     */
    @GetMapping("selectCustomerServesByAssigner")
    @ResponseBody
    public Map<String,Object> selectCustomerServesByAssigner(CustomerServeQuery customerServeQuery,HttpServletRequest request){
        Integer id = LoginUserUtil.releaseUserIdFromCookie(request);
        customerServeQuery.setAssigner(id.toString());
        return customerServeService.queryCustomerServeByParams(customerServeQuery);
    }

    /**
     *  查询服务状态为fw_003，并且处理人是当前用户
     * @param customerServeQuery
     * @return
     */
    @GetMapping("selectCustomerServesByHandler")
    @ResponseBody
    public Map<String,Object> selectCustomerServesByHandler(CustomerServeQuery customerServeQuery,HttpServletRequest request){
        Integer id = LoginUserUtil.releaseUserIdFromCookie(request);
        customerServeQuery.setAssigner(id.toString());
        return customerServeService.queryCustomerServeByParams(customerServeQuery);
    }

    /**
     *  查询服务状态为fw_005的所有服务
     * @param customerServeQuery
     * @return
     */
    @GetMapping("selectCustomerServesByArchive")
    @ResponseBody
    public Map<String,Object> selectCustomerServesByArchive(CustomerServeQuery customerServeQuery){

        return customerServeService.queryCustomerServeByParams(customerServeQuery);
    }


}
